﻿using System;
using System.Windows.Input;

namespace DynamicVm
{
    public abstract class ViewModel : DynamicBindable, ICommandFactory
    {
        protected ViewModel(ICommandFactory factory)
        {
            Contract.NotNull(factory, "factory");
            _factory = factory;
        }

        public T GetValue<T>(string name)
        {
            IMemberValue member = GetMember(name);
            return member == null ? default(T) : member.GetValue<T>();
        }

        public ICommand Command(Action execute, Func<bool> canExecute = null)
        {
            return _factory.Command(execute, canExecute);
        }

        public ICommand Command<T>(Action<T> execute, Func<T, bool> canExecute = null)
        {
            return _factory.Command(execute, canExecute);
        }

        private readonly ICommandFactory _factory;
    }
}
